package com.zhengshi.formal;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class 月收入循环跳20180104 {
//网易基本成型          帐号文件为E:\\ZhangHao/娱乐号.txt
	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException {
		String zhangHaofilepath = "E:\\ZhangHao";// 帐号文件夹路径
		Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
		Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");
		final WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("http://mp.163.com/index.html");
		try {
				for (int z = 0; z < 99999; z++) {
				driver.manage().deleteAllCookies();

				SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy hh:mm:ss a",Locale.US);
				
				String name = "NTES_SESS";
				// 读取帐号文件
				File filename2 = new File(zhangHaofilepath + "/娱乐号.txt"); // 读帐号文件
				InputStreamReader reader2 = new InputStreamReader(new FileInputStream(filename2));
				BufferedReader br2 = new BufferedReader(reader2);
				String line2 = "";
				line2 = br2.readLine().trim();
				String[] s1 = line2.split("\\/", -1);
				String value = s1[3];
				System.out.println("---------------start---------------------");
				System.out.println(s1[0]+"。开始执行巡查任务");
				
				String domain = ".mp.163.com";
				String path = "/";
				Date expiry = sdf.parse("1/30/2028 10:31:37 AM");
				Cookie cookie = new Cookie(name, value, domain, path, expiry);
				driver.manage().addCookie(cookie);	
				Thread.sleep(400);
				for (int i = 0; i < 2; i++) {
					try{
				        //设置超时时间为3S
				        driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS);
				        driver.get("http://mp.163.com/#/income-stat");
				        i=1;
				    }catch(Exception e){
				    	i=0;
				    }
				}
				Thread.sleep(1000);
				driver.findElement(By.xpath("//*[@id='root']/div/div[2]/div/div[2]/div/div/div/div[2]/div[1]/div[2]/div/div[1]/span[5]")).click();;
				
				 Thread.sleep(1000);
				/*//方法一： 
				  //1、操作垂直滚动条，向下移动500像素
				   String setscroll = "document.documentElement.scrollTop=" + "9999999999";  
				   //2、水平滚动条，向右移动500像素
				   //String setscroll = "document.documentElement.scrollLeft=" + "500";  
				   JavascriptExecutor jse=(JavascriptExecutor) driver;  
				   jse.executeScript(setscroll); */
				
				
				/*WebDriverWait wait = new WebDriverWait(driver, 5000);
				//**************等待时间****************
				wait.until(new ExpectedCondition<WebElement>() {
					@Override
					public WebElement apply(WebDriver d) {
						WebElement ddElement = driver.findElement(By.className("isActive"));
						if (ddElement.getText().equals("跟贴管理")) {
							return ddElement;
						} else {
							return null;
						}
					}
				});*/
				driver.manage().deleteAllCookies();
				// 删除第一行
				List<String> list = readFile(zhangHaofilepath + "/娱乐号.txt", line2);
				System.out.println("------------------------------------------");
				System.out.println("还有("+list.size()+")个【帐号】没有查看");
				writeFile(zhangHaofilepath + "/娱乐号.txt", list);
				System.out.println("---------------end---------------------");
				}
				
		} catch (Exception e) {
		   e.printStackTrace();
		}
		 driver.quit();
	};




	// 方法1：

	public void method1() {
		FileWriter fw = null;
		try {
			// 如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f = new File("E:\\dd.txt");
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println("追加内容");
		pw.flush();
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 方法2：

	public static void method2(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	// 方法3：

	public static void method3(String fileName, String content) {
		try {
			// 打开一个随机访问文件流，按读写方式
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			// 文件长度，字节数
			long fileLength = randomFile.length();
			// 将写文件指针移到文件尾。
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static List<String> readFile(String filePath, String str) throws IOException {
		File file = new File(filePath);
		BufferedReader reader = null;
		List<String> list = new ArrayList<String>();
		reader = new BufferedReader(new FileReader(file));
		String text = reader.readLine();
		while (text != null) {
			if (!text.trim().equals(str)) {
				list.add(text + "\r\n");
			}
			text = reader.readLine();
		}
		reader.close();
		return list;
	}

	public static void writeFile(String filePath, List<String> list) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream outputStream = new FileOutputStream(file);
		for (String s : list) {
			outputStream.write(s.getBytes());
		}
		outputStream.close();
	}

}
